using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.ObjectModel;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;

namespace ShareTools.SolutionManagerLibrary
{
    public class Utility
    {
        public List<string> GetWebApplications()
        {
            List<string> webAppList = new List<string>();
            SPWebService ws = SPWebService.ContentService;
            if (ws != null)
            {
                SPWebApplicationCollection webApps = ws.WebApplications;

                foreach (SPWebApplication spWA in webApps)
                {
                    webAppList.Add(spWA.Name);
                }
            }
            return webAppList;
        }

        public Collection<SPWebApplication> GetWebApplicationsCollection()
        {
            SPWebService ws = SPWebService.ContentService;
            SPWebApplicationCollection webApps = ws.WebApplications;
            Collection<SPWebApplication> spwaColl = new Collection<SPWebApplication>();
            foreach (SPWebApplication spWA in webApps)
            {
                spwaColl.Add(spWA);
            }
            return spwaColl;
        }

        public List<string> GetSolutions(bool isDeployedOnly)
        {
            SPSolutionCollection sc = SPFarm.Local.Solutions;
            List<string> solutionsList = new List<string>();
            foreach (SPSolution s in sc)
            {
                try
                {
                    if (isDeployedOnly && s.Deployed)
                        solutionsList.Add(s.Name);
                    else
                        solutionsList.Add(s.Name);
                }
                catch (Exception e1)
                {
                }
            }
            return solutionsList;
        }

        public bool IsNewSolution(string wspFileName)
        {
            // Gets current solution (if present)
            SPSolution solution = SPFarm.Local.Solutions[wspFileName];
            return solution == null ? true : false;
        }

        public void DeploySolution(WSP wsp)
        {
            try
            {
                SPSolution solution = SPFarm.Local.Solutions[wsp.Name];
                if (solution != null)
                    RemoveSolution(wsp);
                solution = SPFarm.Local.Solutions.Add(wsp.Location);
                if (solution.ContainsWebApplicationResource)  /*Added 4.16.09 DMANN  */
                {
                    Collection<SPWebApplication> selectedWebApps = new Collection<SPWebApplication>();
                    if (wsp.UrlList.Count > 0)
                    {
                        SPWebService ws = SPWebService.ContentService;
                        SPWebApplicationCollection webApps = ws.WebApplications;
                        foreach (string url in wsp.UrlList)
                        {
                            selectedWebApps.Add(webApps[url]);
                        }

                        solution.Deploy(wsp.IsImmediate ? DateTime.Now : wsp.TimeToDeploy, wsp.IsGACDeploymentAllowed, selectedWebApps, wsp.IsForced);
                    }
                    else
                        solution.Deploy(wsp.IsImmediate ? DateTime.Now : wsp.TimeToDeploy, wsp.IsGACDeploymentAllowed, wsp.IsForced);
                }
                else
                    solution.Deploy(wsp.IsImmediate ? DateTime.Now : wsp.TimeToDeploy, wsp.IsGACDeploymentAllowed, wsp.IsForced);
            }
            catch (Exception e1)
            {
                throw e1;
            }
            if (wsp.IsImmediate)
                ExecuteJobDefinitions();            
        }

        public void UpgradeSolution(WSP wsp)
        {
            try
            {
                SPSolution solution = SPFarm.Local.Solutions[wsp.Name];
                solution.Upgrade(wsp.Location, wsp.IsImmediate ? DateTime.Now : wsp.TimeToDeploy);
                ExecuteJobDefinitions();
            }
            catch (Exception e1)
            {
                throw e1;
            }
        }



        public void RemoveSolution(WSP wsp)
        {
            try
            {
                SPSolution solution = SPFarm.Local.Solutions[wsp.Name];
                if (solution != null)
                {
                    if (
                        (solution.Deployed)
                        && (solution.ContainsWebApplicationResource)
                        )
                        solution.Retract(DateTime.Now, solution.DeployedWebApplications);
                    else
                        solution.Retract(DateTime.Now);

                    ExecuteJobDefinitions();
                    solution.Delete();
                }
            }
            catch (Exception e1)
            {
                throw e1;
            }
        }

        public void RemoveSolutions(List<WSP> wspList)
        {
            try
            {
                List<SPSolution> spSolList = new List<SPSolution>();
                SPSolution solution = null;
                foreach (WSP wsp in wspList)
                {
                    solution = SPFarm.Local.Solutions[wsp.Name];
                    if (solution != null)
                    {
                        try
                        {
                            if (
                                (solution.Deployed)
                                && (solution.ContainsWebApplicationResource)
                                )
                                solution.Retract(DateTime.Now, solution.DeployedWebApplications);
                            else
                                solution.Retract(DateTime.Now);
                            
                            spSolList.Add(solution);
                        }
                        catch (Exception ex)
                        {

                        }
                    }
                }

                ExecuteJobDefinitions();

                foreach (SPSolution sol in spSolList)
                {
                    sol.Delete();
                }
            }
            catch (Exception e1)
            {
                throw e1;
            }
        }

        public void ExecuteJobDefinitions()
        {
            System.Diagnostics.Process proc = new System.Diagnostics.Process();
            proc.EnableRaisingEvents = false;
            proc.StartInfo.FileName = "runadmjobs.bat";
            proc.StartInfo.UseShellExecute = false;
            proc.StartInfo.CreateNoWindow = true;
            proc.StartInfo.RedirectStandardOutput = false;
            proc.Start();

            //txtInformation.Text = proc.StandardOutput.ReadToEnd();
            proc.WaitForExit();

            //if (SPFarm.Local.TimerService.JobDefinitions != null)
            //{
            //    foreach (SPJobDefinition job in SPFarm.Local.TimerService.JobDefinitions)
            //    {
            //        try
            //        {
            //            job.Execute(SPServer.Local.Id);
            //        }
            //        catch { }
            //    }

            //}
        }
    }
}
